#include "Controller.h"

PNE_NAMESPACE_BEGIN

Controller::Controller()
    // INFO: enum class must explicit convert as base class
    : m_keyStates(static_cast<unsigned int>(Buttons::A)),
      m_keyBind(static_cast<sf::Keyboard::Key>(Buttons::TotalButtons))
{
    // m_keyBind[static_cast<unsigned int>(Buttons::A)] = sf::Keyboard::J;
    // 
}

void Controller::SetKeyBind(
    const std::vector<sf::Keyboard::Key> &keys)
{
    m_keyBind = keys;
}

void Controller::Strobe(Byte b)
{
    m_strobe = (b & 1);
    if (!m_strobe)
    {
        m_keyStates = 0;
        int shift = 0;
        for (int btn = static_cast<int>(Buttons::A); btn < static_cast<int>(Buttons::TotalButtons); ++btn)
        {
            m_keyStates |= (sf::Keyboard::isKeyPressed(m_keyBind[static_cast<sf::Keyboard::Key>(btn)]) << shift);
            ++shift;
        }
    }
}

Byte Controller::Read()
{
    Byte ret;
    if (m_strobe)
        // is this works?
        ret = sf::Keyboard::isKeyPressed(m_keyBind[static_cast<sf::Keyboard::Key>(Buttons::A)]);
    else
    {
        ret = (m_keyStates & 1);
        m_keyStates >>= 1;
    }

    return ret | 0x40;
}

PNE_NAMESPACE_END
// DONE